package com.jarvan.service.impl;

import com.jarvan.enums.RefundEnum;
import com.jarvan.model.Refund;
import com.jarvan.repository.RefundRepository;
import com.jarvan.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RefundServiceImpl implements RefundService {

    @Autowired
    private RefundRepository repository;

    @Override
    public Refund save(Refund refund) {
        return repository.save(refund);
    }

    @Override
    public Refund findById(Integer refundId) {
        return repository.findById(refundId).orElse(null);
    }

    @Override
    public Page<Refund> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public List<Refund> findAllByBuyerAndStatus(Integer buyerId, Integer refundStatus) {
        return repository.findAllByBuyerIdAndRefundStatus(buyerId, refundStatus);
    }

    @Override
    public Refund findByTicket(String ticketId) {
        return repository.findByTicketId(ticketId);
    }

    @Override
    public Refund delete(Integer refundId) {
        Refund refund =  this.findById(refundId);
        refund.setRefundStatus(RefundEnum.REFUND_DELETE.getCode());
        return repository.save(refund);
    }

    @Override
    public Refund recorver(Integer refundId) {
        Refund refund =  this.findById(refundId);
        refund.setRefundStatus(RefundEnum.REFUND_NORMAL.getCode());
        return repository.save(refund);
    }

    @Override
    public Page<Refund> findByStatus(Integer refundStatus, Pageable pageable) {
        return repository.findAllByRefundStatus(refundStatus, pageable);
    }
}
